import sharp from 'sharp'
import fs from 'fs'
import {width,height} from './config'
import path from "node:path";

export async function sharpTest(){
    const semiTransparentRedPng = await sharp({
        create: {
            width: 48,
            height: 48,
            channels: 4,
            background: { r: 255, g: 0, b: 0, alpha: 0.5 }
        }
    })
        .png()
        .toBuffer();

    fs.writeFileSync('./image/sharp-test.png',semiTransparentRedPng)
    return path.resolve('./image/sharp-test.png');
}

export async function sharpTest2(){

    // optimizeSpeed inherit
    const svgText = `
<svg width="${width}" height="${height}">
  <rect width="100%" height="100%" fill="white"/>
  <text  text-rendering="inherit" x="50%" y="50%" font-size="21" text-anchor="middle" fill="black" dy=".3em">你好微软雅黑!</text>
</svg>
`;

    await sharp({
        create: {
            width,
            height,
            channels: 4,
            background: 'white'
        }
    })
        .composite([{ input: Buffer.from(svgText), top: 0, left: 0 }])
        .webp({quality:100})
        .toFormat('png')
        .toFile('./image/sharp-test.svg.png');


    return path.resolve('./image/sharp-test.svg.png');
}
